package dk.mirasola.systemtraining.user.client.ui.table;

import com.google.gwt.cell.client.AbstractEditableCell;
import com.google.gwt.dom.client.Element;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;

public class RadioButtonCell extends AbstractEditableCell<Boolean, Boolean> {
    private SafeHtml radio_checked;
    private SafeHtml radio_unchecked;

    public RadioButtonCell(String name) {
        if (name == null || name.length() == 0) {
            throw new IllegalArgumentException();
        }
        radio_checked = SafeHtmlUtils.fromSafeConstant("<input type=\"radio\" tabindex=\"-1\" name=\"" + name + "\" checked/>");
        radio_unchecked = SafeHtmlUtils.fromSafeConstant("<input type=\"radio\" tabindex=\"-1\" name=\"" + name + "\"/>");
    }

    @Override
    public boolean isEditing(Context context, Element parent, Boolean value) {
        return false;
    }

    @Override
    public void render(Context context, Boolean value, SafeHtmlBuilder sb) {
        if (value != null && value.booleanValue()) {
            sb.append(radio_checked);
        } else {
            sb.append(radio_unchecked);
        }
    }
}
